import { ProForm, ProFormTextArea } from '@ant-design/pro-components';
import { message, Modal } from 'antd';
import React from 'react';
import services from '@/services/game';

const { editCommGameRules, queryCommGameRules } = services.GameController

const handEdit = async (fields: any) => {
  const hide = message.loading('正在更新');
  try {
    const { success, errorMessage } = await editCommGameRules(fields);
    hide();
    if (success) {
      message.success('更新成功');
      return true;
    } else {
      message.error(errorMessage);
      return false;
    }
  } catch (error) {
    hide();
    message.error('更新失败请重试！');
    return false;
  }
}
/**
 * 通用规则
 */
interface Props {
  modalVisible: boolean;
  onCancel: (show: boolean) => void;
}

const GameEditRulesCommForm: React.FC<Props> = (props) => {
  const { modalVisible, onCancel } = props;
  const [context, setContext] = React.useState('')

  React.useEffect(() => {
    queryCommGameRules().then(data => {
      setContext(data.data ?? '')
    })
  }, [])
  return (
    <Modal
      title="通用规则"
      open={modalVisible}
      onCancel={() => onCancel(false)}
      footer={null}
      width="60%"
      height="60%"
      style={{ padding: 0, top: 10 }}
    >
      <ProForm
        onFinish={async (values) => {
          const success = await handEdit(values);
          if (success) {
            onCancel?.(false);
          }
        }}
      >
        {context && <ProFormTextArea
          name="txt"
          placeholder="请输入通用规则"
          fieldProps={
            {
              defaultValue: context
            }
          }
        />}
      </ProForm>
    </Modal>
  );
};

export default GameEditRulesCommForm;
